package com.ksy.common.image;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.request.RequestOptions;
import com.ksy.common.R;


public class ImageLoader {
    private static ImageLoader imageLoader;

    private ImageLoader() {

    }

    public static ImageLoader getInstance() {
        if (imageLoader == null) {
            synchronized (ImageLoader.class) {
                if (imageLoader == null) {
                    imageLoader = new ImageLoader();
                    return imageLoader;
                }
            }
        }
        return imageLoader;
    }

    private boolean contextNull(Context context, ImageView view) {
        if (context == null || view == null) {
            return true;
        }
        if (context instanceof Activity) {
            Activity activity = (Activity) context;
            if (activity.isFinishing() || activity.isDestroyed()) {
                return true;
            }
        }
        return false;
    }

    public void load(Context context, String url, ImageView view, RequestOptions... requestOptions) {
        if (contextNull(context, view)) {
            return;
        }
        RequestBuilder<Drawable> requestBuilder;
        if (TextUtils.isEmpty(url)) {
            requestBuilder = Glide.with(context).load(R.drawable.common_icon_image_placeholder);
        } else {
            requestBuilder = Glide.with(context).load(url);
        }
        requestBuilder = requestBuilder.apply(new ImageOptions().placeholder(R.drawable.common_icon_image_placeholder).error(R.drawable.common_icon_image_placeholder));
        for (int i = 0; i < requestOptions.length; i++) {
            RequestOptions requestOption = requestOptions[i];
            requestBuilder = requestBuilder.apply(requestOption);
        }
        requestBuilder.into(view);
    }


    public void load(Context context, int resId, ImageView view, RequestOptions... requestOptions) {
        if (contextNull(context, view)) {
            return;
        }
        RequestBuilder<Drawable> requestBuilder;
        if (resId < 0) {
            requestBuilder = Glide.with(context).load(R.drawable.common_icon_image_placeholder);
        } else {
            requestBuilder = Glide.with(context).load(resId);
        }
        for (int i = 0; i < requestOptions.length; i++) {
            RequestOptions requestOption = requestOptions[i];
            requestBuilder = requestBuilder.apply(requestOption);
        }
        requestBuilder.into(view);
    }

    public void loadHead(Context context, String url, ImageView view, RequestOptions... requestOptions) {
        if (contextNull(context, view)) {
            return;
        }
        RequestBuilder<Drawable> requestBuilder;
        if (TextUtils.isEmpty(url)) {
            requestBuilder = Glide.with(context).load(R.drawable.common_icon_head);
        } else {
            requestBuilder = Glide.with(context).load(url);
        }
        requestBuilder = requestBuilder.apply(new ImageOptions().placeholder(R.drawable.common_icon_head).error(R.drawable.common_icon_head));
        for (int i = 0; i < requestOptions.length; i++) {
            RequestOptions requestOption = requestOptions[i];
            requestBuilder = requestBuilder.apply(requestOption);
        }
        requestBuilder.into(view);
    }

    public void loadHead(Context context, int resId, ImageView view, RequestOptions... requestOptions) {
        if (contextNull(context, view)) {
            return;
        }
        RequestBuilder<Drawable> requestBuilder;
        if (resId < 0) {
            requestBuilder = Glide.with(context).load(R.drawable.common_icon_head);
        } else {
            requestBuilder = Glide.with(context).load(resId);
        }
        for (int i = 0; i < requestOptions.length; i++) {
            RequestOptions requestOption = requestOptions[i];
            requestBuilder = requestBuilder.apply(requestOption);
        }
        requestBuilder.into(view);
    }
}